import axios from 'axios';
import { getToken } from '@/utils/storage';

const service = axios.create({
  baseURL: import.meta.env.VITE_API_BASE,
  timeout: 5000
});

// 请求拦截器
service.interceptors.request.use(
  config => {
    const token = getToken();
    if (token && config.url.startsWith('/user') && !config.url.includes('/login') && !config.url.includes('/register')) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 支付专用axios实例
const paymentService = axios.create({
  baseURL: import.meta.env.VITE_API_BASE,
  timeout: 15000,
  withCredentials: true
});

// 支付请求拦截器
paymentService.interceptors.request.use(
  config => {
    const token = getToken();
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 高德地图专用axios实例
const mapService = axios.create({
  baseURL: 'https://restapi.amap.com/v3',
  timeout: 5000,
  withCredentials: false
});

export { service as default, paymentService, mapService };